package com.softer.chengxin.traning.dao;

import com.softer.chengxin.person.domin.Person;
import com.softer.chengxin.traning.dao.provider.TraningInfoProvider;
import com.softer.chengxin.traning.entity.TraningInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface TraningInfoDao {
    /**
     * 通过项目id查询所有数据
     *
     * @param projectId
     * @return
     */
    @SelectProvider(type = TraningInfoProvider.class, method = "selectByproject")
    List<Person> selectByproject(@Param("projectId") String projectId);

    /**
     * 查询
     *
     * @param certNo
     * @return
     */
    @Select("select count(1) from traning_info where cert_no=#{certNo}")
    int selectByCertNo(@Param("certNo") String certNo);

    /**
     *单个查询
     *
     * @param id
     * @return
     */
    @Select("SELECT t.id,t.file_id as fileId,t.cert_no as certNo, p.project_name AS projectName, base.`name` AS companyName,person.`name`," +
            "person.education_level AS educationLevel, '项目经理' as titleName FROM `traning_info` AS t" +
            " LEFT JOIN company_person AS person ON person.id = t.person_id" +
            " LEFT JOIN company_base_info AS base ON base.id = person.company_id" +
            " LEFT JOIN company_project AS p ON p.id = t.project_id where t.id=#{id}")
    TraningInfo getInfo(@Param("id") String id);

    /**
     * 查询列表
     */
    @SelectProvider(type = TraningInfoProvider.class, method = "selectAllTraning")
    List<TraningInfo> selectAllTraning(@Param("traningInfo") TraningInfo traningInfo);
    /**
     * 新增
     */
    @Insert("INSERT INTO traning_info(project_id,company_id,cert_no,person_id,create_time," +
            "update_time,file_id,education) VALUES(#{b.projectId},#{b.companyId},#{b.certNo},#{b.personId},now(),now(),#{b.fileId},#{b.education})")
    int addTraning(@Param("b") TraningInfo traningInfo);

    /**
     * 通过登陆人查看审核列表
     * @param traningInfo
     * @return
     */
    @SelectProvider(type = TraningInfoProvider.class, method = "getListByArea")
    List<TraningInfo> getListByArea(@Param("traningInfo") TraningInfo traningInfo);

    /**
     * 修改主表的是否审核的状态
     * @param id
     * @param state
     * @return
     */
    @Update("update traning_info set state=#{state} where id=#{id}")
    int changeState(@Param("id") String id, @Param("state") String state);

    /**
     * 通过登陆人查看全部列表
     * @param traningInfo
     * @return
     */
    @SelectProvider(type = TraningInfoProvider.class, method = "selectStateByArea")
    List<TraningInfo> selectStateByArea(@Param("traningInfo") TraningInfo traningInfo);
    /**
     * 查询名称
     * @param id
     * @return
     */
    @Select("select name from  company_person where id=#{id}")
    String name(String id);
    /**
     * 查询编号
     */
    @Select("select code from project_manager_train where name=#{name}")
    String selectNo(String name);

    /**
     * 查询审核
     * @param no
     * @return
     */
    @Select("select count(0) from traning_info where cert_no=#{no}")
    int selectAllNo(String no);
}
